/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.lucas.controlgasto.statgraphs;

import ar.lucas.controlgasto.dao.GastoDiaDTO;
import ar.lucas.controlgasto.dao.PersonaDAO;
import ar.lucas.controlgasto.resources.Consultas;
import java.awt.BasicStroke;
import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author lucmussi
 */
public class drawGraphsPanel {

       
                
    private JFreeChart createLineChart(final CategoryDataset dataset, boolean allPersonas) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createLineChart(
            "Gastos del Mes",       // chart title
            "Días",                    // domain axis label
            "Monto",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            allPersonas,                      // include legend
            true,                      // tooltips
            false                      // urls
        );

        chart.setBackgroundPaint(Color.white);

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);

        // customise the range axis...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);

        // customise the renderer...
        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
//        renderer.setDrawShapes(true);

        renderer.setSeriesStroke(
            0, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {10.0f, 6.0f}, 0.0f
            )
        );
        renderer.setSeriesStroke(
            1, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {6.0f, 6.0f}, 0.0f
            )
        );
        renderer.setSeriesStroke(
            2, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {2.0f, 6.0f}, 0.0f
            )
        );
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;
    }


    private CategoryDataset createDatasetLine(String nombre,
                                              Collection<GastoDiaDTO> gastos) {
        // row keys...
        final String nombres = nombre;

        // column keys...

        // create the dataset...
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(GastoDiaDTO gasto : gastos){
            dataset.addValue(gasto.getMonto(), nombres, gasto.getDia());
        }
        return dataset;

    }

    private CategoryDataset createDatasetLine(Collection<PersonaDAO> personas, Date fecha) throws SQLException {

        Collection<GastoDiaDTO> gastos = new ArrayList<GastoDiaDTO>();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        String nombres = null;
        for(PersonaDAO persona: personas){
            nombres = persona.getNombre();
            gastos = Consultas.buscarGastosPorMes(persona.getId_persona(), fecha);
            for(GastoDiaDTO gasto: gastos){
                dataset.addValue(gasto.getMonto(), nombres, gasto.getDia());
            }
        }
        return dataset;
    }


    public ChartPanel genLineGraph(int id_persona, Date fecha) {
        String nombre = null;
        Collection<GastoDiaDTO> gastos = null;
        boolean todos = false;
        try {
            // create a dataset...
            nombre = Consultas.buscarPersonaById(id_persona);
            gastos = Consultas.buscarGastosPorMes(id_persona, fecha);
        } catch (SQLException ex) {
            Logger.getLogger(drawGraphsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
            CategoryDataset bardata = createDatasetLine(nombre, gastos);
            JFreeChart chart = createLineChart(bardata,todos);
            // create and display a frame...
            ChartPanel panel = new ChartPanel(chart);
            return panel;
    }

    public ChartPanel genLineGraph(Date fecha){
        // create a dataset...
        boolean todos = true;
        Collection<PersonaDAO> personas;
        CategoryDataset bardata = null;
        try {
            personas = Consultas.buscarAllPersonas();
            bardata = createDatasetLine(personas, fecha);
        } catch (SQLException ex) {
            Logger.getLogger(drawGraphsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        JFreeChart chart = createLineChart(bardata, todos);
        ChartPanel panel = new ChartPanel(chart);
        return panel;
    }

}
